﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpiderHelper.Tools
{
    public class File
    {
        public static string SaveTempFile(Model.TempDoc doc)
        {
            string fileName = "";
            Random rand = new Random();
            string path = AppDomain.CurrentDomain.BaseDirectory.Replace("/", "\\");
            if (!path.EndsWith("\\"))
            {
                path += "\\doc\\";
            }
            else
            {
                path += "doc\\";
            }
            fileName = path + rand.NextDouble().ToString().Replace(".", "") + rand.NextDouble().ToString().Replace(".", "") + ".html";
            fileName.Debug();
            Common.IO.File.WriteHTML(fileName, doc.Content);
            return fileName;
        }
        /// <summary>
        /// 写主路径
        /// </summary>
        /// <param name="links">links的第一个参数是名称，第二个是链接地址</param>
        public static void CreateRootHtml(List<KeyValuePair<string, string>> links, string channelName)
        {
            string fileName = AppDomain.CurrentDomain.BaseDirectory.Replace("/", "\\");
            if (!fileName.EndsWith("\\"))
            {
                fileName += "\\index\\" + channelName + ".html";
            }
            else
            {
                fileName += "index\\" + channelName + ".html";
            }
            System.Text.StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, string> s in links)
            {
                string link = s.Value;
                string[] str = link.Split(new char[] { '#' });
                sb.Append("<a href=\"/doc/" + str[0] + "\">" + s.Key + "</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"" + str[1] + "\" target=\"_blank\">" + str[1] + "</a><br/>");
            }
            Common.IO.File.WriteHTML(fileName, sb.ToString());
        }
    }
}
